package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.model.TaskAttachment;
import ge.telasi.tasks.ui.Platform;
import ge.telasi.tasks.ui.comp.SimpleTable;
import ge.telasi.tasks.ui.comp.TableModel;
import ge.telasi.tasks.ui.file.FileDecorationUtils;
import ge.telasi.tasks.ui.log.LoggerUtils;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

/**
 * @author dimitri
 */
public class AttachmentTable extends SimpleTable<TaskAttachment> {
    private static final long serialVersionUID = 7100261883984215459L;
    static final int ICON = 0;
    static final int FILE_NAME = 1;
    static final int SIZE = 2;
    static final int TYPE = 3;
    static final int CREATED = 4;
    static final int OWNER = 5;
    
    public AttachmentTable() {
        super();
        setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        TableColumnModel colModel = getColumnModel();
        TableColumn colImage = colModel.getColumn(ICON);
        colImage.setResizable(false);
        TableColumn colSize = colModel.getColumn(SIZE);
        colSize.setCellRenderer(new SizeFieldRenderer());
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() > 1) {
                    TaskAttachment selected = getSelection();
                    if (selected != null) {
                        Platform pl = Platform.getDefault();
                        try {
                            byte[] data = pl.getFacade().downloadAttachment(pl.getCredentials(), selected.getTask(), selected);
                            pl.writeAndOpenTempFile(selected.getName(), data);
                            data = null;
                        } catch (Exception ex) {
                            LoggerUtils.manageException(AttachmentTable.this, ex);
                        } finally {
                            System.gc();
                        }
                    }
                }
            }
        });
    }

    @Override
    protected TableModel<TaskAttachment> createEmptyModel() {
        return new AttachmentTableModel();
    }

    @Override
    protected List<TaskAttachment> getDataFromServer(Platform pl) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected String getPreferenceName() {
        return "SimpleTable.AttachmentTable";
    }

    @Override
    protected int[] getDefaultColumnWidthes() {
        return new int[] { 20, 200, 75, 200, 120, 150 };
    }

    class AttachmentTableModel extends TableModel<TaskAttachment> {
        private static final long serialVersionUID = 6797064610463811726L;
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm");

        @Override
        public int getColumnCount() {
            return 6;
        }

        @Override
        public String getColumnName(int column) {
            switch (column) {
                case ICON:
                    return "";
                case FILE_NAME:
                    return "დასახელება";
                case TYPE:
                    return "ტიპი";
                case CREATED:
                    return "დაემატა";
                case OWNER:
                    return "დაამატა";
                case SIZE:
                    return "ზომა";
                default:
                    return "?";
            }
        }

        @Override
        public Class<?> getColumnClass(int column) {
            switch (column) {
                case ICON:
                    return Icon.class;
                case FILE_NAME:
                case TYPE:
                case CREATED:
                case OWNER:
                    return String.class;
                case SIZE:
                    return Integer.class;
                default:
                    return Object.class;
            }
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            switch (column) {
                default:
                    return false;
            }
        }

        @Override
        public Object getValueAt(int row, int column) {
            TaskAttachment binaryData = getObjectAt(row);
            switch (column) {
                case ICON:
                    String ext = FileDecorationUtils.getExtension(binaryData.getName());
                    return FileDecorationUtils.getIcon(ext);
                case FILE_NAME:
                    return binaryData.getName();
                case TYPE:
                    String ext2 = FileDecorationUtils.getExtension(binaryData.getName());
                    return FileDecorationUtils.getTypeDescription(ext2);
                case CREATED:
                    return df.format(binaryData.getCreated().getTime());
                case OWNER:
                    return binaryData.getOwner().getFullName();
                case SIZE:
                    return binaryData.getLength();
                default:
                    return "?";
            }
        }

    }

    class SizeFieldRenderer extends DefaultTableCellRenderer {
        private static final long serialVersionUID = 6985988196043311129L;
        NumberFormat nf = new DecimalFormat("#,##0");

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if (value == null) {
                setText("--");
            } else {
                setText(nf.format(value));
            }
            setHorizontalAlignment(JLabel.RIGHT);
            return this;
        }

    }

}
